//
//  FoundTableView.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/5.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "FoundTableView.h"
#import "FoundTableViewCell.h"
#import "MJRefresh.h"
#import "NetEaseDataService.h"
#import "UIImageView+WebCache.h"

@implementation FoundTableView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.delegate = self;
        self.dataSource = self;
        
        [self addRefreshAction];
        
        [self registerClass:[FoundTableViewCell class] forCellReuseIdentifier:kFoundTableViewCellId];
    }
    
    return self;
}

//头部视图的第一部分
- (void)setBannerArray:(NSArray *)bannerArray {
    if (_bannerArray != bannerArray) {
        _bannerArray = [bannerArray copy];
        [self resetHeaderView];
    }
}
//头部视图的第二部分
- (void)setRecommendArray:(NSArray *)recommendArray {
    if (_recommendArray != recommendArray) {
        _recommendArray = [recommendArray copy];
        [self resetHeaderView];
    }
}
//表视图
- (void)setStreamArray:(NSArray *)streamArray {
    if (_streamArray != streamArray) {
        _streamArray = [streamArray copy];
        [self reloadData];
    }
}

//重新设置头部视图
- (void)resetHeaderView {
    UIView* tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 140)];
    tableHeaderView.userInteractionEnabled = YES;
    
    if (!_bannerArray.count || !_recommendArray.count) {
        tableHeaderView = nil;
        self.tableHeaderView = nil;
        return;
    }
    
    //第一部分
    UIScrollView* scrollView1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 80)];
    scrollView1.contentSize = CGSizeMake(kWidth*_bannerArray.count, 80);
    scrollView1.pagingEnabled = YES;
    scrollView1.showsHorizontalScrollIndicator = NO;
    [tableHeaderView addSubview:scrollView1];
    for (int i = 0; i < _bannerArray.count; i++) {
        NSDictionary* dic = _bannerArray[i];
        UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i*kWidth, 0, kWidth, 80)];
        [imgView sd_setImageWithURL:[NSURL URLWithString:dic[@"image"]]];
        [scrollView1 addSubview:imgView];
    }
    
    //第二部分
    UIScrollView* scrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, kWidth, 60)];
    scrollView2.contentSize = CGSizeMake(kWidth/4*_recommendArray.count, 60);
//    scrollView2.pagingEnabled = YES;
    scrollView2.showsHorizontalScrollIndicator = NO;
    [tableHeaderView addSubview:scrollView2];
    for (int i = 0; i < _recommendArray.count; i++) {
        NSDictionary* dic = _recommendArray[i];
        //图标
        UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(i*kWidth/4+(kWidth/4/2-15), 10, 30, 30)];
        [imgView sd_setImageWithURL:[NSURL URLWithString:dic[@"icon"]]];
        [scrollView2 addSubview:imgView];
        //标题
        UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(i*kWidth/4, imgView.bottom, kWidth/4, 20)];
        label.text = dic[@"title"];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:12];
        [scrollView2 addSubview:label];
        //细线
        if (i) {
            UIImageView* line = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth/4*i, 0, 0.3, 60)];
            UIImage* lingImg = [UIImage imageNamed:@"home_channel_bar_line"];
            [lingImg stretchableImageWithLeftCapWidth:1 topCapHeight:1];
            line.image = lingImg;
            [scrollView2 addSubview:line];
        }
    }
    
    self.tableHeaderView = tableHeaderView;
}

#pragma mark - refreshAction
- (void)addRefreshAction {
    //下拉事件,刷新最新数据
    self.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewestAction)];
}

- (void)loadNewestAction {
    [NetEaseDataService foundDataRequestBlock:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"发现数据 请求失败 %@", result);
            [self.header endRefreshing];
            return;
        }
        //头部视图的信息
        NSArray* bannerArray = result[@"banner"];
        if (bannerArray.count) self.bannerArray = bannerArray;
        NSArray* recommendArray = result[@"recommend"];
        if (recommendArray.count) self.recommendArray = recommendArray;
        //表视图的信息
        NSArray* streamArray = result[@"stream"];
        if (streamArray.count) self.streamArray = streamArray;
    }];
    
    [self.header endRefreshing];
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _streamArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 10)];
    view.backgroundColor = [UIColor grayColor];
    view.alpha = 0.3;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 350;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FoundTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kFoundTableViewCellId forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    
    NSDictionary* dic = _streamArray[indexPath.section];
    cell.dataDic = dic;
    
    return cell;
}

@end
